package cn.iot.sys.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.iot.common.dto.ResultMsg;
import cn.iot.sys.dao.SysDeviceDao;
import cn.iot.sys.entity.SysDevice;
import cn.iot.sys.service.SysDeviceService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.netsdk.demo.module.LoginModule;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class SysDeviceServiceImpl extends ServiceImpl<SysDeviceDao, SysDevice> implements SysDeviceService {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Override
    public ResultMsg addSysDevice(SysDevice sysDevice) {
        logger.info("SysDeviceServiceImpl addSysDevice sysDevice{}",sysDevice);
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String formattedDate = sdf.format(date);
        sysDevice.setCreateTime(formattedDate);
        int count = this.baseMapper.insert(sysDevice);
        if (count>0){
            return new ResultMsg(200,"成功",count);
        }
        return new ResultMsg(500,"失败",null);
    }

    @Override
    public ResultMsg getSysDeviceList(Map<String, Object> map) {
        logger.info("SysDeviceServiceImpl getSysDeviceList map{}",map);
        String code = (String) map.get("code");
        String name = (String) map.get("name");
        String startDate = (String) map.get("startDate");
        String endDate = (String) map.get("endDate");
        String location = (String) map.get("location");
        QueryWrapper<SysDevice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("isDel",0);
        if (StrUtil.isNotEmpty(code)){
            queryWrapper.eq("code",code);
        }
        if (StrUtil.isNotEmpty(name)){
            queryWrapper.eq("name",name);
        }
        if (StrUtil.isNotEmpty(startDate) && StrUtil.isNotEmpty(endDate)){
            queryWrapper.between("createTime", startDate, endDate);
        }
        if (StrUtil.isNotEmpty(location)){
            if (location.contains("/")){
                queryWrapper.eq("location",location);
            }else {
                queryWrapper.like("location",location);
            }

        }
        List<SysDevice> sysDeviceList = this.baseMapper.selectList(queryWrapper);
        map.put("sysDeviceList",sysDeviceList);

        ResultMsg msg = new ResultMsg(200,"成功",map);
        return msg;
    }

    @Override
    public ResultMsg updSysDevice(SysDevice sysDevice) {
        logger.info("SysDeviceServiceImpl updSysDevice sysDevice{}",sysDevice);
        int count = this.baseMapper.updateById(sysDevice);
        if (count>0){
            return new ResultMsg(200,"成功",count);
        }
        return new ResultMsg(500,"失败",null);
    }

    @Override
    public ResultMsg delSysDeviceById(int id) {
        logger.info("SysDeviceServiceImpl delSysDeviceById id{}",id);
        SysDevice sysDevice = new SysDevice();
        sysDevice.setId(id);
        sysDevice.setIsDel(1);
        int count = this.baseMapper.updateById(sysDevice);
        if (count>0){
            return new ResultMsg(200,"成功",count);
        }
        return new ResultMsg(500,"失败",null);
    }

    @Override
    public ResultMsg refreshOnlineStatus(String id) {
        SysDevice sysDevice = new SysDevice();
        sysDevice.setId(Integer.parseInt(id));
        sysDevice.setStatus(1);
        LoginModule.logout();
        LoginModule.init(null, null);
        boolean isLogin = LoginModule.login("192.168.1.198", 37777, "admin", "Admin123456");
        if (isLogin){
            int count = this.baseMapper.updateById(sysDevice);
            if (count>0){
                return new ResultMsg(200,"成功",null);
            }
            return new ResultMsg(500,"失败",null);
        }
        return new ResultMsg(500,"失败",null);
    }
}
